/**
 *  @author cainli
 *  @d2013-4-19 @tim上午11:21:42
 */
package com.cainli.szbus.data.operation;

import java.util.ArrayList;

import android.os.Bundle;

import com.cainli.szbus.data.factory.BusLineListJsonFactory;
import com.cainli.szbus.data.model.BusLine;
import com.cainli.szbus.data.requestmanager.RequestFactory;
import com.cainli.szbus.log.CLog;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.network.NetworkConnection.ConnectionResult;
import com.foxykeep.datadroid.requestmanager.Request;

/**
 * @author cainli
 *
 */
public class BusLineListOperation extends BaseOperation {
	private static final String TAG = BusLineListOperation.class.getSimpleName();
	public static final String PARAM_CITY = "city";
	public static final String PARAM__KEYWORD = "q";
	public static final String PARAM_WITH_XYS = "with_xys";
	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getType()
	 */
	@Override
	protected String getType() {
		return "lines";
	}

	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getParams(com.foxykeep.datadroid.requestmanager.Request)
	 */
	@Override
	protected void initParams(Request request) {
		params.put(PARAM_CITY, request.getString(PARAM_CITY));
		params.put(PARAM__KEYWORD, request.getString(PARAM__KEYWORD));
		params.put(PARAM_WITH_XYS, String.valueOf(request.getInt(PARAM_WITH_XYS)));

	}

	/* (non-Javadoc)
	 * @see com.cainli.szbus.data.operation.BaseOperation#getParseResult(com.foxykeep.datadroid.network.NetworkConnection.ConnectionResult)
	 */
	@Override
	protected Bundle getParseResult(ConnectionResult result) {
		Bundle bundle = new Bundle();
		ArrayList<BusLine> busLineList;
		try {
			busLineList = BusLineListJsonFactory
					.parseResult(result.body);
			bundle.putParcelableArrayList(RequestFactory.BUNDLE_EXTRA_BUSLINE_LIST, busLineList);
		} catch (DataException e) {
			CLog.d(TAG, e.getMessage());
		}
		return bundle;
	}
	

/*	private void addDB(Context context, ArrayList<BusLine> personList)
			throws DataException {
		// Clear the table
        context.getContentResolver().delete(DbPerson.CONTENT_URI, null, null);

        // Adds the persons in the database
        int personListSize = personList.size();
        if (personListSize > 0) {
            ArrayList<ContentProviderOperation> operationList =
                    new ArrayList<ContentProviderOperation>();

            for (int i = 0; i < personListSize; i++) {
                operationList.add(ContentProviderOperation.newInsert(DbPerson.CONTENT_URI)
                        .withValues(personList.get(i).toContentValues()).build());
            }

            try {
                context.getContentResolver().applyBatch(PoCProvider.AUTHORITY, operationList);
            } catch (RemoteException e) {
                throw new DataException(e);
            } catch (OperationApplicationException e) {
                throw new DataException(e);
            }
        }
	}*/
}
